# -*- coding: utf-8 -*-

from zaneapp.management import commands


class CommandError(Exception):
    def __str__(self):
        return "command error"


class Command:
    """
    工具类指令
    """
    def __init__(self, sub_command, *args):
        self.sub_command = sub_command
        self.args = args

    def execute(self):
        try:
            cmd = getattr(commands, self.sub_command)
            cmd.execute(*self.args)
        except:
            modes = dir(commands)
            lines = ["\t" + m + "\n" for m in modes if not m.startswith("_")]
            first_line = "args error\nargs:\n"
            print("".join([first_line, ] + lines))


def execute_commands(*args):
    if len(args) < 2:
        raise CommandError()
    sub_command = args[1]
    comm = Command(sub_command, *tuple(args[2:]))
    comm.execute()
